﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLACQUY.BUS;
using System.Collections;
using System.IO;
using QLACQUY.DTO;

namespace QLACQUY
{
    public partial class ThemSanPham : Form
    {
        private bool _isUpdate;
        private int _maSP;
        public int MaSP
        {
            get { return _maSP; }
            set { _maSP = value; }
        }
        public bool IsUpdate
        {
            get { return _isUpdate; }
            set { _isUpdate = value; }
        }
        public ThemSanPham()
        {
            InitializeComponent();
            _isUpdate = false;
        }

        private void btnThemNhaSanXuat_Click(object sender, EventArgs e)
        {
            ThemNhaSanXuat frm = new ThemNhaSanXuat();
            frm.ShowDialog();
            //refresh combo box
            getComboBoxNSXData();
            //cmbNhaSanXuat.Update();

        }

        private void btnThenQuyCach_Click(object sender, EventArgs e)
        {
            ThemQuyCach frm = new ThemQuyCach();
            frm.ShowDialog();
            //refresh combo box            
            getComboBoxQuyCachData();            
            //ThemSanPham_Load();
            //cmbQuyCach.Update();
        }

        private void ThemSanPham_Load(object sender, EventArgs e)
        {
            getComboBoxNSXData();
            getComboBoxQuyCachData();
            if(_isUpdate)
            {
                this.Text = "CapNhatSanPham";
                btnOK.Enabled = false;
                btnOK.Visible = false;
                btnCapNhat.Visible = true;
                SanPhamBUS bus = new SanPhamBUS();
                SanPhamCRT crt = bus.layThongTinSanPham(_maSP);
                txtDonGiaLe.Text = crt.DonGiaLe.ToString();
                txtDonGiaSy.Text = crt.DonGiaSy.ToString();
                txtMoTa.Text = crt.MoTa.ToString();
                //pictureBox1.Visible = true;
                //pictureBox1.ImageLocation = Global.appFolder + crt.HinhAnh;
                txtHinhAnh.Text = Global.appFolder + crt.HinhAnh;
                cmbNhaSanXuat.Text = crt.NSX;
                cmbQuyCach.Text = crt.QuyCach;
                this.AcceptButton = btnCapNhat;
            }
            
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
        private void getComboBoxNSXData()
        {
            //read data from database and bind to combo box nhasanxuat
            //cmbNhaSanXuat.ResetText();
            NhaSanXuatBUS bus = new NhaSanXuatBUS();
            DataTable tb = bus.layBang();
            cmbNhaSanXuat.DataSource = tb;
            cmbNhaSanXuat.DisplayMember = "TenNSX";
            cmbNhaSanXuat.ValueMember = "MaNSX";
            this.Refresh();
            //cmbNhaSanXuat.Refresh();
            
            
        }
        private void getComboBoxQuyCachData()
        {
            //read data from database and bind to combo box quycach
            //cmbQuyCach.ResetText();
            QuyCachBUS bus = new QuyCachBUS();
            DataTable tb = bus.layBang();
            cmbQuyCach.DataSource = tb;
            cmbQuyCach.DisplayMember = "QuyCachSanPham";
            cmbQuyCach.ValueMember = "QuyCachSanPham";
            
            //cmbQuyCach.Refresh();

        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "JPG files (*.jpg)|*.jpg|All files (*.*)|*.*";
            dlg.RestoreDirectory = true;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                txtHinhAnh.Text = dlg.FileName;
                lbImageName.Text = dlg.SafeFileName;
            }
        }
        private int inputChecking()
        {
            //check intput
            errorProvider1.Clear();
            int err = 0;
            int temp;
            if (txtDonGiaLe.Text == "" || !int.TryParse(txtDonGiaLe.Text, out temp))
            {
                errorProvider1.SetError(txtDonGiaLe, "Không hợp lệ");
                err = 1;
            }
            if (txtDonGiaSy.Text == "" || !int.TryParse(txtDonGiaSy.Text, out temp))
            {
                errorProvider1.SetError(txtDonGiaSy, "Không hợp lệ");
                err = 1;
            }
            //if (txtHinhAnh.Text == "")
            //{
            //    errorProvider1.SetError(txtHinhAnh, "Không được để trống!");
            //    err = 1;
            //}
            if (txtMoTa.Text == "")
            {
                errorProvider1.SetError(txtMoTa, "Không được để trống!");
                err = 1;
            }


            return err;
        }
        private void btnOK_Click(object sender, EventArgs e)
        {

            if (inputChecking() != 0)
                return;
            
            //if (!System.IO.Directory.Exists(Global.appFolder))
                //System.IO.Directory.CreateDirectory(Global.appFolder);

            SanPhamDTO dto = new SanPhamDTO();
            try
            {
                //File.Copy(txtHinhAnh.Text, Global.appFolder + lbImageName.Text, true);
                dto.TenSanPham = "";
                dto.QuyCach = (string)cmbQuyCach.SelectedValue;
                dto.MaNSX = (int)cmbNhaSanXuat.SelectedValue;
                dto.HinhAnh = "";// lbImageName.Text;
                dto.MoTa = txtMoTa.Text;
                dto.DonGiaLe = int.Parse(txtDonGiaLe.Text);
                dto.DonGiaSy = int.Parse(txtDonGiaSy.Text);

                SanPhamBUS bus = new SanPhamBUS();
                int kq = bus.themSanPham(dto);
                if (kq > 0)
                {
                    MessageBox.Show("Thêm thành công!");
                    ResetForm();
                }
                

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void ResetForm()
        {
            txtDonGiaLe.Text = "";
            txtDonGiaSy.Text = "";
            txtHinhAnh.Text = "";
            txtMoTa.Text = "";
            txtDonGiaSy.Focus();
        }
        

        private void cmbQuyCach_Click(object sender, EventArgs e)
        {
            getComboBoxQuyCachData();
        }

        private void cmbNhaSanXuat_Click(object sender, EventArgs e)
        {
            getComboBoxNSXData();
        }

        private void txtHinhAnh_MouseClick(object sender, MouseEventArgs e)
        {
            btnBrowse_Click(null, null);
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            if (inputChecking() != 0)
                return;

            //if (!System.IO.Directory.Exists(Global.appFolder))
                //System.IO.Directory.CreateDirectory(Global.appFolder);

            SanPhamDTO dto = new SanPhamDTO();
            try
            {
                //if (!File.Exists(Global.appFolder + lbImageName.Text))
                    //File.Copy(txtHinhAnh.Text, Global.appFolder + lbImageName.Text, true);
                dto.MaSanPham = _maSP;
                dto.TenSanPham = "";
                dto.QuyCach = (string)cmbQuyCach.SelectedValue;
                dto.MaNSX = (int)cmbNhaSanXuat.SelectedValue;
                dto.HinhAnh = "";// lbImageName.Text;
                dto.MoTa = txtMoTa.Text;
                dto.DonGiaLe = int.Parse(txtDonGiaLe.Text);
                dto.DonGiaSy = int.Parse(txtDonGiaSy.Text);

                SanPhamBUS bus = new SanPhamBUS();
                int kq = bus.capNhatSanPham(dto);
                if (kq > 0)
                {
                    MessageBox.Show("Cập nhật thành công!");
                    this.Dispose();
                }


            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

       
    }
}
